<?php
namespace Home\Controller;
use Think\Controller\RestController;
/**
 * 任务管理控制器
 * @author    王金鹏 <firKin@126.com>
 */
class MissionController extends CommonController {
    /**
     *
     */
    public function index(){

		$uid = session('uid');
		$role = M('user_dept_role')->where("user_id = $uid")->select();
		$roleId = $role[0]['role_id'];
		$this->assign('roleId', $roleId);

		$dept_id = $role[0]['dept_id'];
		$members = M('user')
			->join('LEFT JOIN user_dept_role ON user.user_id = user_dept_role.user_id')
			->where("dept_id = $dept_id")
			->select();

		foreach ($members as $value) {
			$member[] = array(
				'std_name' => $value['user_name'],
				'id' => $value['user_id']
			);
		}
		$jsmember = json_encode($member);
		//dump($jsmember);die;
		$this->assign('jsMember', $jsmember);

		$this->display();
	}

	public function mission(){
		$uid = session('uid');
		$time = time();
		$workUp = M('workup');

		//已经完成的任务ID
		$finished_id = $workUp->where("up_user_id = $uid")
							  ->getField("up_work_id",true);

		//已经完成的任务
		$finished = array();
		if ($finished_id == null) {
			$finished_id = array(
				0 => null,
				);
		}else{
			$i=0;
			foreach ($finished_id as $value) {
				$work = M('work')->where("work_id = $value")->find();

				//找到发布者
				$publisher_id = $work['work_publisher'];
	 			$publisher = M('user')->where("user_id = $publisher_id")
	 								  ->getField("user_name",true);

					$finished[] = array(
							'state' => '已完成',
							'work_id' => $work['work_id'],
							'title' => $work['work_title'],
							'publisher' => $publisher[0],
							'summary' => $work['work_introduce'],
							'details' => $work['work_content'],
							'score' => '',
							'people' => '',
							'time' => array(
	 									'begin' => date('Y-m-d', $work['work_create_time']),
	 									'end' => $work['work_deadline_time']
	 								  ),
							'suggest' => '',
						);

				//找到任务相关的人
				if ($work['work_dept_id'] == NULL && $work['work_role_id'] == NULL) {

					$workId = $work['work_id'];
		 			$people = M('user_work')->where("uw_work_id = $workId")
		 									->getField("uw_user_id",true);
		 			$memberArr = array();
		 			foreach ($people as $v) {
		 				$member = M('user')->where("user_id = $v")
		 								   ->getField("user_name",true);
		 				$memberArr[] = $member;
		 			}
		 			$finished[$i]['people'] = array();
		 			foreach ($memberArr as $va) {
		 				$finished[$i]['people'][] = $va[0];
		 			}
				}else{
					$workRoleId = $work['work_role_id'];
					$workDeptId = $work['work_dept_id'];
					$deptName = M('department')->where("dept_id = $workDeptId")
								   ->getField('dept_name', true);
					$roleName = M('role')->where("id = $workRoleId")
										 ->getField('remark', true);
					$finished[$i]['people'] = array();
					$finished[$i]['people'][] = $deptName[0].$roleName[0];
				}

				//找到分数和评论
				$workId = $work['work_id'];
		 		$score = M('workup')->where("up_work_id = $workId")
		 							->getField("up_score",true);
		 		$suggest = M('workup')->where("up_work_id = $workId")
		 							->getField("up_suggest",true);
		 		$finished[$i]['score'] = $score[0];
		 		$finished[$i]['suggest'] = $suggest[0];
			}
			$i++;
		}

		$Work = M('work');
		$allWork_id = $Work
					->join("LEFT JOIN user_work ON work.work_id = user_work.uw_work_id")
					->join("LEFT JOIN user_dept_role ON user_dept_role.user_id = $uid")
					->where("user_work.uw_user_id = $uid OR (work_role_id = role_id AND work_dept_id = dept_id)")
					->getField("work_id",true);

		$left_id = array_diff($allWork_id,$finished_id);//剩下的分待完成和已经过期的
		$unfished_id = array();//待完成任务
		$expire_id = array();//过期任务

		foreach ($left_id as $value) {
			$time = strtotime($Work->where("work_id = $value")->getField('work_deadline_time'));
			if ($time > time()){
				array_push($unfished_id, $value);
			}else {
				array_push($expire_id, $value);
			}
		}

		$unfished = array();
		if ($unfished_id == null) {
			$unfished_id = array(
				0 => null,
				);
		}else{
			$i = 0;
			foreach ($unfished_id as $value) {
				$work = M('work')->where("work_id = $value")->find();

				//找到发布者
				$publisher_id = $work['work_publisher'];
	 			$publisher = M('user')->where("user_id = $publisher_id")
	 								  ->getField("user_name",true);

				$unfished[] = array(
						'state' => '未完成',
						'work_id' => $work['work_id'],
						'title' => $work['work_title'],
						'publisher' => $publisher[0],
						'summary' => $work['work_introduce'],
						'details' => $work['work_content'],
						'people' => '',
						'time' => array(
	 								'begin' => date('Y-m-d', $work['work_create_time']),
	 								'end' => $work['work_deadline_time']
	 							  ),
							);

				//找到任务相关的人
				if ($work['work_dept_id'] == NULL && $work['work_role_id'] == NULL) {
					$workId = $work['work_id'];
		 			$people = M('user_work')->where("uw_work_id = $workId")
		 									->getField("uw_user_id",true);
		 			$memberArr = array();
		 			foreach ($people as $v) {
		 				$member = M('user')->where("user_id = $v")
		 								   ->getField("user_name",true);
		 				$memberArr[] = $member;
		 			}
		 			$unfished[$i]['people'] = array();
		 			foreach ($memberArr as $va) {
		 				$unfished[$i]['people'][] = $va[0];
		 			}
				}else{
					$workRoleId = $work['work_role_id'];
					$workDeptId = $work['work_dept_id'];
					$deptName = M('department')->where("dept_id = $workDeptId")
								   ->getField('dept_name', true);
					$roleName = M('role')->where("id = $workRoleId")
										 ->getField('remark', true);
					$unfished[$i]['people'] = array();
					$unfished[$i]['people'][] = $deptName[0].$roleName[0];
				}
			$i++;
			}
		}
		$expire = array();
		if ($expire_id == null) {
			$expire_id = array(
				0 => null,
				);
		}else{
			$i=0;
	 		foreach ($expire_id as $value) {
	 			$work = M('work')->where("work_id = $value")
	 							 ->find();
	 			$publisher_id = $work['work_publisher'];
	 			$publisher = M('user')->where("user_id = $publisher_id")
	 								  ->getField("user_name",true);

	 			$expire[$i]['state'] = '已过期';
	 			$expire[$i]['work_id'] = $work['work_id'];
	 			$expire[$i]['title'] = $work['work_title'];
	 			$expire[$i]['publisher'] = $publisher[0];
	 			$expire[$i]['summary'] = $work['work_introduce'];
	 			$expire[$i]['details'] = $work['work_content'];
	 			$expire[$i]['people'] ='';
	 			$expire[$i]['time'] = array(
	 								'begin' => date('Y-m-d', $work['work_create_time']),
	 								'end' => $work['work_deadline_time']
	 							);
	 			//找到任务相关的人
	 			if ($work['work_dept_id'] == NULL && $work['work_role_id'] == NULL) {
		 			$workId = $work['work_id'];
		 			$people = M('user_work')->where("uw_work_id = $workId")
		 									->getField("uw_user_id",true);
		 			$memberArr = array();
		 			foreach ($people as $v) {
		 				$member = M('user')->where("user_id = $v")
		 								   ->getField("user_name",true);
		 				$memberArr[] = $member;
		 			}
		 			$expire[$i]['people'] = array();
		 			foreach ($memberArr as $va) {
		 				$expire[$i]['people'][] = $va[0];
		 			}
	 			}else{
	 				$workRoleId = $work['work_role_id'];
					$workDeptId = $work['work_dept_id'];
					$deptName = M('department')->where("dept_id = $workDeptId")
								   ->getField('dept_name', true);
					$roleName = M('role')->where("id = $workRoleId")
										 ->getField('remark', true);
					$expire[$i]['people'] = array();
					$expire[$i]['people'][] = $deptName[0].$roleName[0];
	 			}
	 			$i++;
	 		}
	 	}

	 	if ($finished != null || $unfished != null) {
	 		foreach ($finished as $value) {
		 		$total[] = $value;
		 	}
		 	foreach ($unfished as $value) {
		 		$total[] = $value;
		 	}
	 	}else{
	 		$total = array();
	 	}

 		$ff = $finished;
 		$uu = $unfished;
 		$ee = $expire;
 		$this->ajaxReturn(array(
 							'id' => $uid,
 							'type' => 'mission',
 							'total' => $total,
 							'unfinished' => $uu,
 							'finished' => $ff,
 							'expire' => $ee,
 						),'json');
	}

	public function publish(){

		$userId = I('id');
		$deptId = M('user_dept_role')->where("user_id = $userId")
									 ->getField('dept_id', true);

		$data = array(
			'work_publisher' => I('id'),
			'work_title' => I('title'),
			'work_introduce' => I('summary'),
			'work_content' => I('details'),
			'work_create_time' => time(),
			'work_deadline_time' =>	I('Endtime'),
			);
		$workId = M('work')->add($data);

		if (I('context') == 0) {			//	如果是 0 就具体到人
			$people = array();
			$people = I('people');
			$data = array();
			for ($i=0; $i <count($people); $i++) {
				$data = array(
					'uw_work_id' => $workId,
					'uw_user_id' => $people[$i],
					);
				M('user_work')->add($data);
			}
		}elseif (I('context') == 2) {		//	2就表示全部学员
			$data = array(
				'work_role_id' => 2,
				'work_dept_id' => $deptId[0]
				);
			M('work')->where("work_id = $workId")->data($data)->save();
		}elseif (I('context') == 3) {		//	3就表示全体干事
			$data = array(
				'work_role_id' => 3,
				'work_dept_id' => $deptId[0]
				);
			M('work')->where("work_id = $workId")->data($data)->save();
		}elseif (I('context') == 4) {		//	4表示学员加干事
			$data = array(
				'work_role_id' => 4,
				'work_dept_id' => $deptId[0]
				);
			M('work')->where("work_id = $workId")->data($data)->save();
		}

		$this->ajaxReturn(array(
			'id' => $userId,
			'type' => 'publish',
			'state' => '200'
			),'json');
	}

	public function submit(){
		$allWork = M('work')->select();
		if (IS_GET) {
			if (!I('get.workId')) {
				$missions = array();
				foreach ($allWork as $value) {
					$workId = $value['work_id'];
					$workUpNum = M('workup')->where("up_work_id = $workId")->count();
					$missions[] = array(
						'workId' => $value['work_id'],
						'title' => $value['work_title'],
						'Endtime' => $value['work_deadline_time'],
						'state' => $workUpNum
						);
				}
				$data = array(
					'id' => session('uid'),
					'type' => 'submit',
					'missions' => $missions
					);
				$this->ajaxReturn($data, 'json');
			}

			$workId = I('get.workId');
			$workUp = M('workup')->where("up_work_id = $workId")->select();
			$missionState = array();
			foreach ($workUp as $value) {
				$userId = $value['up_user_id'];

				$userName = M('user')->where("user_id = $userId")
									 ->getField('user_name', true);
				$missionState[] = array(
					'std_name' => $userName[0],
					'std_time' => $value['up_create_time'],
					'score' => $value['up_score'],
					'suggest' => $value['up_suggest']
					);
			}
			$data = array(
					'id' => session('uid'),
					'type' => 'submit',
					'missionState' => $missionState
					);
				$this->ajaxReturn($data, 'json');
		}
	}
}